package com.vcredit.creditcat.web.app;

import com.opensymphony.oscache.util.StringUtil;
import com.vcredit.creditcat.bean.ReferenceCustManager;
import com.vcredit.creditcat.bean.Session;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.redis.RedisServiceExt;
import com.vcredit.framework.controller.BaseController;
import com.vcredit.framework.util.VerifyCodeUtil;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * Controller父类，被app下的Controller继承
 * @author zhaoqi01
 *
 */
public class BaseCreditCatController extends BaseController {

	@Value("${project.env}")
	private String env;
	@Autowired
	private RedisServiceExt redisService;
	@Autowired
	private ReferenceCustManager referenceCustManager;

	private static Integer SMS_EXPIRE_TIME = 60 * 10;

	protected Session getSession(HttpServletRequest request) {
		return (Session) request.getSession().getAttribute("session");
	}

	/**
	 *  判断是否登陆
	 * @param request
	 * @return
	 */
	protected boolean hasOnLine(HttpServletRequest request) {
		boolean online = false;
		String accessToken =request.getHeader("accessToken");
		if (StringUtil.hasLength(accessToken)) {
			 online = true;
		}
		return online;
	}

	protected Cust getCust(HttpServletRequest request) {
		Cust cust = this.getSession(request).getCust();
		cust = referenceCustManager.findCustById(cust.getCustId());
		this.getSession(request).setCust(cust);
		return cust;
	}

	protected void clearSession(HttpServletRequest request) {
		request.getSession().removeAttribute("session");
	}

	protected Long getAccountId(HttpServletRequest request) {
		return this.getSession(request).getAccountId();
	}

	/**
	 * 校验验证码，排除超级用户13999999999不校验
	 * @author zhaoqi01
	 * @param request
	 * @param mobile
	 * @param code
	 * @param businessType
	 * @return
	 */
	protected Boolean checkVerifyCode(HttpServletRequest request, String mobile, String code, Integer businessType) {
		if (!StringUtils.equalsIgnoreCase("prd", env)) {
			return true;
		} else {
			if("13999999999".equals(mobile)){
				return true;
			}
			String key = "vercode:"+request.getRequestedSessionId() + "_" + mobile + "_" + businessType;
			Object object = redisService.get(key);
			if (object == null) {
				return false;
			} else {
				return object.equals(code);
			}
		}
	}

	/**
	 * 创建验证码
	 * 
	 * @param request
	 * @param mobile
	 *            手机
	 * @param businessType
	 *            验证类型 1:登陆 2:绑定手机 3:绑定银行卡
	 * @return
	 */
	protected String createVerifyCode(HttpServletRequest request, String mobile, Integer businessType) {
		String code = VerifyCodeUtil.getVerifyCode(true).getCode();
		String key = "vercode:"+request.getRequestedSessionId() + "_" + mobile + "_" + businessType;
		if (redisService.get(key) != null) {
			redisService.del(key);
		}
		redisService.put(key, code, SMS_EXPIRE_TIME);
		return code;
	}

	/**
	 * 获取request参数 封装Map
	 * 
	 * @author 李小强
	 */
	protected Map<String, String> getAllRequestParam(HttpServletRequest request) {
		Map<String, String> res = new HashMap<String, String>();
		Enumeration<?> temp = request.getParameterNames();
		if (null != temp) {
			while (temp.hasMoreElements()) {
				String en = (String) temp.nextElement();
				String value = request.getParameter(en);
				res.put(en, value);
				if (res.get(en) == null || "".equals(res.get(en))) {
					res.remove(en);
				}
			}
		}
		return res;
	}

}